//
//  CLLockLineView.m
//  CoreLock
//
//  Created by WCImApp on 2019/8/13.
//  Copyright © 2019 冯成林. All rights reserved.
//

#import "CLLockLineView.h"
#import "CLLockItemView.h"
#import "CoreLockConst.h"

@implementation CLLockLineView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    //数组为空直接返回
    if(_itemViewsM == nil || _itemViewsM.count == 0) return;
    
    //获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    //添加路径
    CGContextAddRect(ctx, rect);
    
    //    //添加圆形路径
    //    //遍历所有的itemView
    //    [_itemViewsM enumerateObjectsUsingBlock:^(CLLockItemView *itemView, NSUInteger idx, BOOL *stop) {
    //        CGContextAddEllipseInRect(ctx, itemView.frame);
    //    }];
    //
    //剪裁
    CGContextEOClip(ctx);
    
    //新建路径：管理线条
    CGMutablePathRef pathM = CGPathCreateMutable();
    
    //设置上下文属性
    //1.设置线条颜色
    [CoreLockLockLineColor set];
    
    //线条转角样式
    CGContextSetLineCap(ctx, kCGLineCapRound);
    CGContextSetLineJoin(ctx, kCGLineJoinRound);
    
    //设置线宽
    CGContextSetLineWidth(ctx, 2.0f);
    
    //遍历所有的itemView
    [_itemViewsM enumerateObjectsUsingBlock:^(CLLockItemView *itemView, NSUInteger idx, BOOL *stop) {
        CGPoint directPoint = itemView.center;
        
        if (idx == 0) {
            //第一个
            //添加起点
            CGPathMoveToPoint(pathM, NULL, directPoint.x, directPoint.y);
        } else {
            //其他
            //添加路径线条
            CGPathAddLineToPoint(pathM, NULL, directPoint.x, directPoint.y);
        }
    }];
    
    //将路径添加到上下文
    CGContextAddPath(ctx, pathM);
    //渲染路径
    CGContextStrokePath(ctx);
    //释放路径
    CGPathRelease(pathM);
}

@end
